package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserTalk;
import com.lantosec.hmrp.modules.archives.mapper.UserTalkMapper;
import com.lantosec.hmrp.modules.archives.service.IUserTalkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserTalkVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.List;

/**
 * <p>
 * 领导干部廉政谈话情况表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-12
 */
@Service
@Primary
@Log4j2
public class UserTalkServiceImpl extends ServiceImpl<UserTalkMapper, UserTalk> implements IUserTalkService {

    @Override
    public void saveUserTalk(UserTalkVO vo) {
        UserTalk userTalk = vo.convertToEntity();
        userTalk.setUserTalkId(UUIDUtils.get());
        //获取当前用户id和名称
        userTalk.setCreateId(TokenUtil.getUserId());
        userTalk.setCreateUser(TokenUtil.getName());
        userTalk.setCreateTime(DateUtils.nowDate());
        userTalk.setVerifyStatus(1);
        this.save(userTalk);
    }

    @Override
    public void updateUserTalk(UserTalkVO vo) {
        String userTalkId = vo.getUserTalkId();
        //新增一条数据
        //saveUserTalk(vo);
        //修改这条数据的审核状态为2
        if (StringUtils.isNotEmpty(userTalkId)) {
            UserTalk userTalk = vo.convertToEntity();
            userTalk.setVerifyStatus(1);
            this.updateById(userTalk);
        }else {
            saveUserTalk(vo);
        }
    }

    @Override
    public List<UserTalk> getUserTalk(String userId) {
        List<UserTalk> userTalkList = this.list(new QueryWrapper<UserTalk>().eq(UserTalk.ColName.user_id.toString(),userId).orderByDesc(UserTalk.ColName.create_time.toString()));
        return userTalkList;
    }

    @Override
    public List<UserTalk> getUserTalkVerify(String userId) {
        List<UserTalk> userTalkList = this.list(new QueryWrapper<UserTalk>().eq(UserTalk.ColName.user_id.toString(),userId).in(UserTalk.ColName.verify_status.toString(),0,2));
        return userTalkList;
    }

    @Override
    public void verifyUserTalk(UserTalkVO vo) {
        UserTalk userTalk = vo.convertToEntity();
        Integer verifyStatus = userTalk.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userTalk.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userTalk.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userTalk.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userTalk.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userTalk.setVerifyId(TokenUtil.getUserId());
        userTalk.setVerifyUser(TokenUtil.getName());
        userTalk.setVerifyTime(DateUtils.nowDate());
        this.updateById(userTalk);
    }

    @Override
    public List<UserTalk> getUserTalkHistory(String userId) {
        List<UserTalk> userTalkList = this.list(new QueryWrapper<UserTalk>().eq(UserTalk.ColName.user_id.toString(),userId).eq(UserTalk.ColName.verify_status.toString(),4));
        return userTalkList;
    }
}
